
#import <misckit/misckit.h>
#import "../Alexandra.h"
#import "PreferencesManager.h"
#import "PMCompose.h"
#import "PMArticleText.h"
#import "PMArticleList.h"
#import "PMGeneral.h"
#import <dbkit/DBTableView.h>

@class Article;

@implementation PreferencesManager

//-----------------------------------------------------------
// INIT
//-----------------------------------------------------------

+ initialize
	{
	static NXDefaultsVector myDefaults = {
				{DEFAULT_ARTICLE_FONT,"Courier 12pt"},
		        {DEFAULT_QUOTING_PREFIX,">"},
				{DEFAULT_HEADER_FILTER,"From:Organization:Subject:Date:"},
				{DEFAULT_APPEND_SIG,"NO"},
				{DEFAULT_SHOW_SUBJECT,"YES"},
				{DEFAULT_SHOW_AUTHOR,"YES"},
				{DEFAULT_SHOW_SIZE,"YES"},
				{DEFAULT_SMALL_ARTICLE,"25"},
				{DEFAULT_LARGE_ARTICLE,"250"},
				{DEFAULT_QUICK_KILL,"NO"},
				{DEFAULT_KEY_BASE,"emacs"},
				{DEFAULT_KEY_BINDINGS,""},
				{DEFAULT_XHEADER_COUNT,"7"},
				{DEFAULT_KILL_BEHAVIOUR,"2"},
				{DEFAULT_SIG_DETECTION,"YES"},
				{DEFAULT_REWRAP_ARTICLE_TEXT,"YES"},
				{DEFAULT_HEADER_MODE,"2"},
				{DEFAULT_CHECKNN_ACTIVATION,"NO"},
				{DEFAULT_CHECKNN_PERIOD,"0"},
				{DEFAULT_SAVE_PATH,"/tmp"},
		        {NULL} };
	const char	*fontname=[NXApp defaultValue:"NXFixedPitchFont"],
				*fontsize=[NXApp defaultValue:"NXFixedPitchFontSize"];
	char		buf[1000];
	
	if(fontname)
		{
		if(fontsize)
			sprintf(buf,"%s %spt",fontname,fontsize);
		else
			sprintf(buf,"%s 12pt",fontname);
		myDefaults[0].value=NXCopyStringBuffer(buf);
		}
	[NXApp registerDefaults:myDefaults];
	return self;
	}


- init
	{
	[super init];
	
	list=[[MiscList alloc] init];
	pHeaderController=[[[MiscTableController alloc] init] setDelegate:self];
	aHeaderController=[[[MiscTableController alloc] init] setDelegate:self];
	[ERROR_MANAGER addObserver:self selector:@selector(externalUpdate:)
						forError:ENOTEPrefsChanged];
	return self;
	}


- awakeFromNib
	{
	if(aHeaders)
		{
		NXAtom	ids[]={[KVPair keyIdentifier],NULL};
		
		[aHeaderController setTableView:aHeaders withIdentifiers:ids];
		}
	if(pTableView)
		{
		NXAtom	ids[]={[KVPair keyIdentifier],[KVPair valueIdentifier],NULL};

		[pHeaderController setTableView:pTableView withIdentifiers:ids];
		}
	return self;
	} 


//-----------------------------------------------------------
// FREE 
//-----------------------------------------------------------

- free
	{
	list=[list free];
	pHeaderController=[pHeaderController free];
	aHeaderController=[aHeaderController free];
	return [super free];
	}



//-----------------------------------------------------------
// PREFERENCES PANEL
//-----------------------------------------------------------

- openPanel:sender;
	{
	if(!thePanel)
		[NXApp loadNibSection:"Preferences.nib" owner:self];
	if(![thePanel isVisible])
		[self readPreferences:self];	
	[thePanel makeKeyAndOrderFront:sender];
	return self;
	}


- readPreferences:sender;
	{
	[self readGeneralPrefs];
	[self readArticleListPrefs];
	[self readArticleTextPrefs];
	[self readPostingPrefs];
	[thePanel setDocEdited:NO];
	[thePanel display];
	return self;
	}


- writePreferences:sender;
	{
	[self writePostingPrefs];
	[self writeArticleTextPrefs];
	[self writeArticleListPrefs];
	[self writeGeneralPrefs];
	[thePanel setDocEdited:NO];
	EM_ERROR(ENOTEPrefsChanged,self,NULL);
	EM_ERROR(ENOTEPrefsChanged2,self,NULL);

	return self;				
	}


- markDirty:sender;
	{
	[thePanel setDocEdited:YES];
	return self;	
	}

- showPathSelectPanel:sender
{
	id panel =[OpenPanel new];

	[panel allowMultipleFiles:NO];
	[panel chooseDirectories:YES];
	
	[panel setDirectory:[gDefaultSavePath stringValue]];
	
	if([panel runModal]!=NX_CANCELTAG){
		[gDefaultSavePath setStringValue:[panel filename]]; //!!!
		[self markDirty:self];
	}
	
	[panel chooseDirectories:NO];
	
	return self;
}
	

//-----------------------------------------------------------
//	EXTERNAL UPDATES
//-----------------------------------------------------------

- externalUpdate:(EMErrorInfo *)info;
	{
	if([info userData1]!=self)
		{
		if([thePanel isDocEdited])
			EM_ERROR(EPREFShouldMerge,NULL,NULL);
		[self readPreferences:nil];
		}
	return self;
	}

	

//-----------------------------------------------------------
// DELEGATE METHODS 
//-----------------------------------------------------------

#define STRG_DIDNT_SET_PREF NXLocalString("You have made changes to your preferences, but not yet applied them.  Do you want to set them now or revert back to their old settings?",NULL,"Frage wenn Pref-Fenster geschlossen werden soll, obwohl noch nicht auf set geklickt wurde.")

#define STRG_ALERT_CANCEL NXLocalString("Cancel",NULL,"In Alert-Panels")
#define STRG_ALERT_REVERT NXLocalString("Revert",NULL,"In Alert-Panels")
#define STRG_ALERT_SET    NXLocalString("Set",   NULL,"In Alert-Panels")


- textDidGetKeys:sender isEmpty:(BOOL)flag
	{
	return [self markDirty:sender]; 
	}


- windowWillClose:(Window *)sender
	{
	int	ret;
	
	if([thePanel isDocEdited])
		{
		ret=NXRunAlertPanel("ALEXANDRA",STRG_DIDNT_SET_PREF,STRG_ALERT_SET,
						STRG_ALERT_REVERT,STRG_ALERT_CANCEL);
		if(ret==NX_ALERTOTHER)
			return nil;
		if(ret==NX_ALERTDEFAULT)
			[self writePreferences:self];
		}
	return self;
	}

//-----------------------------------------------------------
// THAT'S IT
//-----------------------------------------------------------

@end
